/**
 * <p>Copyright: Copyright (c) 2014</p>
 * <p>Company: ---Technology Co. Ltd</p>
 * <p>2014-4-30</p>
 * @author Jan.Zhang
 */
package com.xianl.busiplat.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.google.common.collect.Lists;
import com.xianl.busiplat.dwz.AjaxObject;
import com.xianl.busiplat.dwz.Page;
import com.xianl.busiplat.po.Module;
import com.xianl.busiplat.po.Role;
import com.xianl.busiplat.service.ModuleService;
import com.xianl.busiplat.service.RoleService;


/** 
 * desc:
 * <p>author: Jan.Zhang </p>
 * date: 2014-4-30
 */
@Controller
@RequestMapping("/sys/core/role")
public class RoleController {

	private static final Logger logger = LoggerFactory.getLogger(RoleController.class);
	@Autowired
	private RoleService roleService;
	@Autowired
	private ModuleService moduleService;
	private static final String CREATE = "sys/core/role/create";
	private static final String UPDATE = "sys/core/role/update";
	private static final String LIST = "sys/core/role/list";
	private int ind = 0;
	private int level = 0;

	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:25:54</p>
	 * @param map
	 * @return
	 */
	@RequiresPermissions("Role:save")
	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public String preCreate(Map<String, Object> map) {
		map.put("module", moduleService.getTree());
		return CREATE;
	}

	// 重新组装permissions（切分test:save,test:edit的形式）
	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:25:57</p>
	 * @param role
	 */
	private void refactor(Role role) {
		List<String> allList = Lists.newArrayList();
		List<String> list = role.getPermissions();
		for(String string: list) {
			if(StringUtils.isBlank(string) ) {
				continue;
			}
			if(string.contains(",") ) {
				String[] arr = string.split(",");
				allList.addAll(Arrays.asList(arr));
			} else {
				allList.add(string);
			}
		}
		role.setPermissions(allList);
	}

	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:26:01</p>
	 * @param role
	 * @return
	 */
	@RequiresPermissions("Role:save")
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public @ResponseBody
	String create(Role role) {
		AjaxObject ajaxObject = new AjaxObject("角色添加成功！");
		refactor(role);
		roleService.save(role);
		ajaxObject.setCallbackType(AjaxObject.CALLBACK_TYPE_SMPRELOADLIST);
		ajaxObject.setForwardUrl(LIST);
		return ajaxObject.toString();
	}

	// @RequiresPermissions("Role:edit")
	// @RequestMapping(value="/update/{id}", method=RequestMethod.GET)
	// public String preUpdate(@PathVariable Long id, Map<String, Object> map) {
	// Role role = roleService.get(id);
	//
	// map.put("module", moduleService.getTree());
	// map.put("role", role);
	// return UPDATE;
	// }
	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:26:04</p>
	 * @param id
	 * @param map
	 * @return
	 */
	@RequiresPermissions("Role:edit")
	@RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
	public String preUpdate(@PathVariable Integer id, Map<String, Object> map) {
		Role role = roleService.get(id);
		List<String> permissions = role.getPermissions();
		Module module = moduleService.getTree();
		StringBuilder sb = new StringBuilder();
		sb.append("<ul>").append("\n");
		level = 0;// 重设全局变量
		getUpdateTree(module.getChildren(), sb, permissions);
		sb.append("</ul>").append("\n");
		System.out.println("---------------------");
		System.out.println(sb.toString());
		map.put("roleName", role.getName());
		map.put("roleId", role.getId());
		map.put("description", role.getDescription());
		map.put("moduleName", module.getName());
		map.put("_html", sb.toString());
		return UPDATE;
	}

	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:26:09</p>
	 * @param list
	 * @param sb
	 * @param permissions
	 */
	private void getUpdateTree(List<Module> list, StringBuilder sb, List<String> permissions) {
		level++;
		Module m = null;
		for(int i = 0; i < list.size(); i++) {
			m = list.get(i);
			if(i == list.size() - 1 ) {
				sb.append("<li class=\"last\">").append("\n");
			} else {
				sb.append("<li>").append("\n");
			}
			sb.append("<div class=\"\"><div class=\"indent\"></div>");
			for(int l = 1; l < level; l++) {
				sb.append("<div class=\"line\"></div>");
			}
			if(!m.getChildren().isEmpty() ) {
				sb.append("<div class=\"collapsable\"></div>").append("\n");
			} else {
				sb.append("<div class=\"node\"></div>").append("\n");
			}
			apen(sb, permissions, m);
			sb.append("</div>").append("\n");
			if(!m.getChildren().isEmpty() ) {
				sb.append("<ul>").append("\n");
				getUpdateTree(m.getChildren(), sb, permissions);
				sb.append("</ul>").append("\n");
			}
			sb.append("</li>").append("\n");
		}
	}

	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:26:17</p>
	 * @param sb
	 * @param permissions
	 * @param m
	 */
	private void apen(StringBuilder sb, List<String> permissions, Module m) {
		ind++;
		sb.append("<a href=\"#\" class=\"permissionList\">").append("\n");
		sb.append("<span class=\"module_name\">").append(m.getName()).append("</span>").append("\n");
		sb.append("<span class=\"inputValue\">").append("\n");
		sb.append("	<input type=\"checkbox\" name=\"permissions[").append(ind).append("]\" value=\"")
				.append(m.getServName()).append(":view\"")
				.append(permissions.contains(m.getServName() + ":view") ? "checked=\"checked\"" : "").append("/>")
				.append("\n");
		sb.append("	<input type=\"checkbox\" name=\"permissions[").append(ind).append("]\" value=\"")
				.append(m.getServName()).append(":save\"")
				.append(permissions.contains(m.getServName() + ":save") ? "checked=\"checked\"" : "").append("/>")
				.append("\n");
		sb.append("	<input type=\"checkbox\" name=\"permissions[").append(ind).append("]\" value=\"")
				.append(m.getServName()).append(":edit\"")
				.append(permissions.contains(m.getServName() + ":edit") ? "checked=\"checked\"" : "").append("/>")
				.append("\n");
		sb.append("	<input type=\"checkbox\" name=\"permissions[").append(ind).append("]\" value=\"")
				.append(m.getServName()).append(":delete\"")
				.append(permissions.contains(m.getServName() + ":delete") ? "checked=\"checked\"" : "").append("/>")
				.append("\n");
		sb.append("	<input type=\"checkbox\" class=\"checkboxCtrl\" group=\"permissions[" + ind + "]\"")
				.append(permissions.contains(m.getServName() + ":view") && permissions.contains(m.getServName() + ":save")
						&& permissions.contains(m.getServName() + ":edit")
						&& permissions.contains(m.getServName() + ":delete") ? "checked=\"checked\"" : "").append("/>")
				.append("\n");
		sb.append("</span>").append("\n");
		sb.append("</a>").append("\n");
	}

	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:26:21</p>
	 * @param role
	 * @return
	 */
	@RequiresPermissions("Role:edit")
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody
	String update(Role role) {
		refactor(role);
		roleService.update(role);
		AjaxObject ajaxObject = new AjaxObject("角色修改成功！");
		ajaxObject.setCallbackType(AjaxObject.CALLBACK_TYPE_SMPRELOADLIST);
		ajaxObject.setForwardUrl(LIST);
		return ajaxObject.toString();
	}

	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:26:26</p>
	 * @param id
	 * @return
	 */
	@RequiresPermissions("Role:delete")
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody
	String delete(@PathVariable Integer id) {
		AjaxObject ajaxObject = new AjaxObject("角色删除成功！");
		if(id == 1 ) {
			logger.warn("操作员{}，尝试删除超级管理员角色", SecurityUtils.getSubject().getPrincipal() + "。");
			ajaxObject.setMessage("不能删除超级管理员角色！");
		} else {
			roleService.delete(id);
		}
		ajaxObject.setCallbackType("");
		return ajaxObject.toString();
	}

	/**
	 * desc: <p>创建人：Jan.Zhang , 2013-1-14 下午04:26:29</p>
	 * @param page
	 * @param keywords
	 * @param map
	 * @return
	 */
	@RequiresPermissions("Role:view")
	@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
	public String list(Page page, String keywords, Map<String, Object> map) {
		List<Role> roles = null;
		if(StringUtils.isNotBlank(keywords) ) {
			Role role = new Role();
			role.setName(keywords);
			roles = roleService.findAll(role, page);
		} else {
			roles = roleService.findAll(null,page);
		}
		map.put("page", page);
		map.put("roles", roles);
		map.put("keywords", keywords);
		return LIST;
	}
}
